import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/components/layout/IndexView.vue'
import {useUserStore} from '@/stores/user'

export const routes = [
    {
        path: '/login',
        component: () => import('@/views/login/indexView.vue'),
        hidden: true
    },
    {
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                component: () => import('@/views/dashboard/indexView.vue'),
                name: 'Dashboard',
                meta: { title: '控制台', icon: 'dashboard' }
            }
        ]
    },
    {
        path: '/admin-articles',
        component: Layout,
        redirect: '/admin-articles/list',
        meta: { title: '管理员文章', icon: 'document' },
        children: [
            {
                path: 'list',
                component: () => import('@/views/admin-articles/ListView.vue'),
                name: 'AdminArticleList',
                meta: { title: '文章列表' }
            },
            {
                path: 'create',
                component: () => import('@/views/admin-articles/EditView.vue'),
                name: 'CreateArticle',
                meta: { title: '发布文章' }
            },
            {
                path: 'edit/:id',  // 添加编辑页面路由
                component: () => import('@/views/admin-articles/EditView.vue'),
                name: 'AdminEditArticle',
                meta: { title: '编辑文章' },
                props: true  // 将路由参数作为组件的 props
            },
            {
                path: 'drafts',
                component: () => import('@/views/admin-articles/DraftsView.vue'),
                name: 'ArticleDrafts',
                meta: { title: '草稿箱' }
            }
        ]
    },
    {
        path: '/crawler-articles',
        component: Layout,
        redirect: '/crawler-articles/list',
        meta: { title: '爬虫文章', icon: 'connection' },
        children: [
            {
                path: 'list',
                component: () => import('@/views/crawler-articles/ListView.vue'),
                name: 'CrawlerArticleList',
                meta: { title: '爬取文章' }
            },
            {
                path: 'tasks',
                component: () => import('@/views/crawler-articles/TasksView.vue'),
                name: 'CrawlerTasks',
                meta: { title: '爬虫任务' }
            },
            {
                path: 'edit/:id',  // 添加编辑页面路由
                component: () => import('@/views/crawler-articles/EditView.vue'),
                name: 'CrawlerEditArticle',
                meta: { title: '编辑文章' },
                props: true  // 将路由参数作为组件的 props
            }
        ]
    },
    {
        path: '/system',
        component: Layout,
        redirect: '/system/params',
        meta: { title: '系统设置', icon: 'setting' },
        children: [
            {
                path: 'params',
                component: () => import('@/views/system/ParamsView.vue'),
                name: 'SystemParams',
                meta: { title: '系统参数' }
            },
            {
                path: 'tags',
                component: () => import('@/views/tag/TagManager.vue'),
                name: 'SystemTags',
                meta: { title: '文章标签管理' }
            },
            {
                path: 'images',
                component: () => import('@/views/system/ImageView.vue'),
                name: 'SystemImages',
                meta: { title: '图片资源管理' }
            },
            {
                path: 'profile',
                component: () => import('@/views/system/ProfileView.vue'),
                name: 'UserProfile',
                meta: { title: '个人信息' }
            }
        ]
    }
]

const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 路由守卫
router.beforeEach(async (to, from, next) => {
    const userStore = useUserStore()

    if (userStore.token) {
        if (to.path === '/login') {
            next({ path: '/' })
        } else {
            // 如果没有用户信息，先获取用户信息
            if (!userStore.hasUserInfo) {
                try {
                    await userStore.getUserInfo()
                } catch (error) {
                    // 获取用户信息失败，可能是token过期，需要重新登录
                    await userStore.logout()
                    next(`/login?redirect=${to.path}`)
                    return
                }
            }
            next()
        }
    } else {
        if (to.path === '/login') {
            next()
        } else {
            next(`/login?redirect=${to.path}`)
        }
    }
})

export default router
